<?php

namespace app\common\model;

use think\Model;


class CouponLog extends Model
{





    // 表名
    protected $name = 'coupon_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'state_text'
    ];



    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getStateList()
    {
        return ['0' => __('State 0'), '1' => __('State 1'), '2' => __('State 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }



    //  领券优惠券
    //  type：领取方式:1=新人优惠券,2=实名认证
    public static function getCoupon($user_id, $type = 1)
    {
        $coupons = Coupon::all(['type' => $type, 'state' => '1', 'num' => ['>', 0]]);
        if ($coupons) {
            foreach ($coupons as $k => $coupon) {
                $data['user_id'] = $user_id;
                $data['coupon_id'] = $coupon['id'];
                if ($coupon['workrange']) {
                    $workrange = explode(' - ', $coupon['workrange']);
                    $data['startdate'] = $workrange[0];
                    $data['enddate'] = $workrange[1];
                }
                $data['name']=$coupon['name'];
                $data['type']=$coupon['type'];
                $data['price']=$coupon['price'];
                $data['money']=$coupon['money'];
                for ($i = 0; $i < $coupon['num']; $i++) {
                    self::create($data, true);
                }
            }
        }
    }
    //  检查优惠券是否能使用
    public static function useCoupon($money, $coupon_id, $user_id)
    {
        $coupon = self::get(['id' => $coupon_id, 'user_id' => $user_id]);
        if ($coupon['state'] == 0) {
            exception('优惠券已过期');
        }
        if ($coupon['state'] == 2) {
            exception('优惠券已使用');
        }
        if ($money <= $coupon['money']) {
            exception('优惠券最低使用金额￥' . $coupon['money']);
        }
        if ($coupon['enddate']) {
            if (date('Y-m-d') > $coupon['enddate']) {
                exception('有效截止日期：' . $coupon['enddate']);
            }
        }
        if (date('Y-m-d') < $coupon['startdate']) {
            exception('有效开始日期：' . $coupon['startdate']);
        }
        if ($money > $coupon['price']) {
            return $coupon['price'];
        } else {
            return $money;
        }
    }
    public static function editState($user_id)
    {
        return  self::where('user_id', $user_id)->whereTime('enddate', '<', date('Y-m-d'))->where('state', 1)->update(['state' => 0]) ? true : false;
    }
    //可使用优惠券列表
    public static function getUseCoupon($type,  $user_id,$limit)
    {
        self::editState($user_id);
        $model = new self;
        if ($type != 'all') {
            $model->where('state', $type);
        }
        $list = $model->where('user_id', $user_id)->order('price desc')->paginate($limit);
        return $list;
    }
}
